#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "protocol_examples_common.h"
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_tls_crypto.h"
#include <esp_http_server.h>
#include "driver/gpio.h"

#include "ledc.h"
#include "touch_button.h"

#include "nvs_storage.h"
#include "smart_config.h"
#include "wifi_connect.h"
#include "server.h"
#define   VERSION     "1.0.0"

static const char *TAG = "main";
/* 主任务 */
void app_main(void)
{
    nvs_storage_init();

    ledc_init();
    set_led_type(COMMON_CATHODE);
    ledc_set_RGB(0,0,0);
    touch_button_init();
    gpio_set_direction(2,GPIO_MODE_OUTPUT);
    gpio_set_level(2,1);
    ESP_LOGI(TAG,"Irir-light - Author :liankafohali - Version :"VERSION" - for Gabrielle!");
    // 初始化日志缓冲池
    // wifi_data_t data = {
    //     .ssid = "CMCC-zuAU",
    //     .ssid_length = 33,
    //     .password = "3tt94adjc",
    //     .passwd_length = 65,
    // };
    // save_wifi_data(data);
    wifi_data_t data;
    if(read_wifi_data(&data) == ESP_OK){
        ESP_LOGI(TAG,"ssid:%s,length:%d,passwd:%s,length:%d\n",data.ssid,data.ssid_length,data.password,data.passwd_length);
    }
    if(wifi_init_sta(data)!= ESP_OK){
        initialise_wifi();     
    }        
    // 主循环
    while (1) {
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
}

